package com.campus.dao.impl;

import com.campus.dao.TeachingCallingDao;
import com.campus.dto.TeachingCalling;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class TeachingCallingDaoImpl implements TeachingCallingDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int addTeachingCalling(TeachingCalling calling) {
        String sql = "INSERT INTO t_teaching_calling (start_time, end_time, ended, creator, class_id) " +
                "VALUES (?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql,
                calling.getStartTime(),
                calling.getEndTime(),
                calling.getEnded(),
                calling.getCreator(),
                calling.getClassId());
    }

    @Override
    public int deleteTeachingCallingById(int id) {
        String sql = "DELETE FROM t_teaching_calling WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int updateTeachingCalling(TeachingCalling calling) {
        String sql = "UPDATE t_teaching_calling SET start_time = ?, end_time = ?, ended = ?, creator = ?, class_id = ? " +
                "WHERE id = ?";
        return jdbcTemplate.update(sql,
                calling.getStartTime(),
                calling.getEndTime(),
                calling.getEnded(),
                calling.getCreator(),
                calling.getClassId(),
                calling.getId());
    }

    @Override
    public List<TeachingCalling> findAllCallings() {
        String sql = "SELECT * FROM t_teaching_calling";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TeachingCalling.class));
    }

}
